//
//  TeachManagementCell.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/27.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachManagementCell.h"
#import "UIImageView+WebCache.h"
@interface TeachManagementCell()
@property (nonatomic,weak) UIImageView *headImgView;
@property (nonatomic,weak) UILabel *nameLabel;
@property (nonatomic,weak) UILabel *phoneLabel;
//@property (nonatomic,weak) UIButton *agreeBtn;
@property (nonatomic,weak) UIButton *phoneBtn;
@property (nonatomic,weak) UILabel *lineLabel;
@property (nonatomic,weak) UILabel *repeatLabel;
@property (nonatomic,weak) UILabel *timeLabel;
@property (nonatomic,weak) UILabel *numLabel;
@property (nonatomic,weak) UILabel *stateLabel;
@property (nonatomic,weak) UILabel *shadowLabel;
@property (nonatomic,assign) NSInteger judgeNum;
@end
@implementation TeachManagementCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier WithNum:(NSInteger)num WithArray:(NSArray *)subjectTwoSourceArr{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.judgeNum = num;
        [self createSubViewWithArray:subjectTwoSourceArr];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubViewWithArray:(NSArray *)subjectTwoSourceArr{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *headImgView = [[UIImageView alloc]init];
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 110*SCALEUNIT, 110*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(55*SCALEUNIT, 55*SCALEUNIT)];
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.frame = CGRectMake(0, 0, 55*SCALEUNIT, 55*SCALEUNIT);
    shapeLayer.path = bezierPath.CGPath;
    headImgView.layer.mask = shapeLayer;
    _headImgView = headImgView;
    [self addSubview:self.headImgView];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont systemFontOfSize:LoginFont];
    nameLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    _nameLabel = nameLabel;
    [self addSubview:self.nameLabel];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.textAlignment = NSTextAlignmentLeft;
    phoneLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    phoneLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    _phoneLabel = phoneLabel;
    [self addSubview:self.phoneLabel];
    
//    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [agreeBtn setBackgroundImage:[UIImage imageNamed:@"TeachManagementAgreeImage"] forState:UIControlStateNormal];
//    _agreeBtn = agreeBtn;
//    [self addSubview:self.agreeBtn];
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [phoneBtn setBackgroundImage:[UIImage imageNamed:@"TeachManagementTelephoneImage"] forState:UIControlStateNormal];
    [phoneBtn addTarget:self action:@selector(phoneOpen:) forControlEvents:UIControlEventTouchUpInside];
    _phoneBtn = phoneBtn;
    [self addSubview:self.phoneBtn];
    
    UILabel *lineLabel = [[UILabel alloc] init];
    lineLabel.backgroundColor = RGBA(244, 244, 244, 1);
    _lineLabel = lineLabel;
    [self addSubview:self.lineLabel];
    
    UILabel *repeatLabel = [[UILabel alloc] init];
    repeatLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    repeatLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    repeatLabel.textAlignment = NSTextAlignmentLeft;
    _repeatLabel = repeatLabel;
    [self addSubview:self.repeatLabel];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    timeLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    _timeLabel = timeLabel;
    [self addSubview:self.timeLabel];
    
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    numLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    numLabel.textAlignment = NSTextAlignmentLeft;
    _numLabel = numLabel;
    [self addSubview:self.numLabel];
    
    UILabel *stateLabel = [[UILabel alloc] init];
    stateLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    stateLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    stateLabel.textAlignment = NSTextAlignmentLeft;
    _stateLabel = stateLabel;
    [self addSubview:self.stateLabel];
    
    UILabel *shadowLabel = [[UILabel alloc] init];
    shadowLabel.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    _shadowLabel= shadowLabel;
    [self addSubview:self.shadowLabel];
    
    if (self.judgeNum==2) {
        TeachManagementButtonsInCellScrollView *buttonsInCellScrollView = [[TeachManagementButtonsInCellScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) WithArray:subjectTwoSourceArr];
        _buttonsInCellScrollView = buttonsInCellScrollView;
        [self addSubview:self.buttonsInCellScrollView];
    }
    
    //布局
    WS(ws);
    [self.headImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(ws).with.offset(30*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(110*SCALEUNIT, 110*SCALEUNIT));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.headImgView).with.offset(-5*SCALEUNIT);
        make.left.mas_equalTo(ws.headImgView.mas_right).with.offset(10*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(325*SCALEUNIT, 64*SCALEUNIT));
    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.headImgView).with.offset(5*SCALEUNIT);
        make.left.mas_equalTo(ws.nameLabel);
        make.size.mas_equalTo(CGSizeMake(325*SCALEUNIT, 40*SCALEUNIT));
    }];
//    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(ws).with.offset(-40*SCALEUNIT);
//        make.centerY.mas_equalTo(ws.nameLabel);
//        make.size.mas_equalTo(CGSizeMake(162*SCALEUNIT, 64*SCALEUNIT));
//    }];
    [self.phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws).with.offset(-95*SCALEUNIT);
        make.centerY.mas_equalTo(ws.headImgView);
        make.size.mas_equalTo(CGSizeMake(54*SCALEUNIT, 54*SCALEUNIT));
    }];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws);
        make.top.mas_equalTo(ws.headImgView.mas_bottom).with.offset(28*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(720*SCALEUNIT, 2*SCALEUNIT));
    }];
    [self.repeatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).with.offset(35*SCALEUNIT);
        make.top.mas_equalTo(ws.lineLabel.mas_bottom).with.offset(20*SCALEUNIT);
        make.right.mas_equalTo(ws).with.offset(-35*SCALEUNIT);
        make.height.mas_equalTo(41*SCALEUNIT);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.repeatLabel.mas_bottom);
        make.left.right.height.mas_equalTo(ws.repeatLabel);
    }];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.timeLabel.mas_bottom);
        make.left.right.height.mas_equalTo(ws.timeLabel);
    }];
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.numLabel.mas_bottom);
        make.left.right.height.mas_equalTo(ws.timeLabel);
    }];
    [self.shadowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(ws);
        make.height.mas_equalTo(20*SCALEUNIT);
    }];
    if (self.judgeNum==2) {
        [self.buttonsInCellScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(ws.stateLabel.mas_bottom).with.offset(20*SCALEUNIT);
            make.centerX.mas_equalTo(ws);
            make.size.mas_equalTo(CGSizeMake(615*SCALEUNIT, 95*SCALEUNIT));
        }];
    }
}

- (NSMutableAttributedString *)returnAttritubeWithTitleOne:(NSString *)titleOne WithTitleTwo:(NSString *)titleTwo WithContentOne:(NSString *)contentOne WithContentTwo:(NSString *)contentTwo WithJudge:(BOOL)judge{
    NSString *content = judge?[NSString stringWithFormat:@"%@:%@  |  %@:%@",titleOne,contentOne,titleTwo,contentTwo]:[NSString stringWithFormat:@"%@:%@",titleOne,contentOne];
    //初始化
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
    //设置范围
    NSRange rangeOne = [content rangeOfString:[NSString stringWithFormat:@"%@:",titleOne]];
    //为某一范围内文字设置多个属性
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:                           [UIColor colorWithHexString:@"#999999"], NSForegroundColorAttributeName,                           [UIFont systemFontOfSize:LoginFont-3], NSFontAttributeName, nil];
    [str setAttributes:attrs range:rangeOne];
    if (judge) {
        NSRange rangeTwo;
        rangeTwo = [content rangeOfString:[NSString stringWithFormat:@"|  %@:",titleTwo]];
        [str setAttributes:attrs range:rangeTwo];
    }
    return str;
}

- (void)setModel:(TeachManagementModel *)model{
    _model = model;
    if (model.loadSate) return;
    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:_model.imgPath] placeholderImage:[UIImage imageNamed:@"MineBoyImage"]];
    self.nameLabel.text = _model.studentName;
    self.phoneLabel.text = [NSString stringWithFormat:@"联系电话:%ld",_model.studentPhone];
    self.repeatLabel.attributedText = [self returnAttritubeWithTitleOne:@"培训类型" WithTitleTwo:@"培训次数" WithContentOne:_model.trainType==1?@"初次培训":@"复训" WithContentTwo:[NSString stringWithFormat:@"%ld",_model.trainCount] WithJudge:YES];
    self.timeLabel.attributedText = [self returnAttritubeWithTitleOne:@"分入时间" WithTitleTwo:@"最近练车时间" WithContentOne:_model.asignCoachDatetime==nil?@"无":[[_model.asignCoachDatetime componentsSeparatedByString:@" "] objectAtIndex:0] WithContentTwo:_model.lastPractiseDatetime==nil?@"无":[[_model.lastPractiseDatetime componentsSeparatedByString:@" "] objectAtIndex:0] WithJudge:YES];
    self.numLabel.attributedText = [self returnAttritubeWithTitleOne:@"本次培训练车次数" WithTitleTwo:@"合计培训练车次数" WithContentOne:[NSString stringWithFormat:@"%ld次",_model.totalPractiseCount] WithContentTwo:[NSString stringWithFormat:@"%ld次",_model.practiseCount] WithJudge:YES];
    self.stateLabel.attributedText = [self returnAttritubeWithTitleOne:@"状态" WithTitleTwo:@"" WithContentOne:_model.curStatus==1?@"未报名":_model.curStatus==2?@"已报名":@"考试通过" WithContentTwo:@"" WithJudge:NO];
    if (self.judgeNum==2) {
        self.buttonsInCellScrollView.ID = _model.ID;
        self.buttonsInCellScrollView.trainItem = _model.trainItem;
        self.buttonsInCellScrollView.studentName = _model.studentName;
    }
}

#pragma mark ==========拨打电话==========
- (void)phoneOpen:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(teachManagementCellDelegatePhoneWithNum:)]) {
        [self.delegate teachManagementCellDelegatePhoneWithNum:[NSString stringWithFormat:@"%ld",_model.studentPhone]];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
